package com.keymozy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.keymozy.mapper.StaffMapper;
import com.keymozy.pojo.Position;
import com.keymozy.pojo.Staff;
import com.keymozy.pojo.dto.UpdPwd;
import com.keymozy.service.LoginService;
import com.keymozy.service.PositionService;
import com.keymozy.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Service
public class ILoginService implements LoginService {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private PositionService positionService;

    @Override
    public String login(String phone, String psw) {
        LambdaQueryWrapper<Staff> lqw = new LambdaQueryWrapper();
        lqw.eq(Staff::getPhone,phone);
        lqw.eq(Staff::getPsw,psw);

        Staff staff = staffMapper.selectOne(lqw);
        LambdaQueryWrapper<Position> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Position::getId,staff.getPid());
        if (staff != null){
            //将重要信息抹去
            staff.setPhone(staff.getPhone().substring(0,3)+"********");
            staff.setPsw("");
            staff.setPosition(positionService.getOne(lqw2));
            //获取token
            String token = TokenUtil.getToken(staff);
            return token;
        }
        return null;
    }

    @Override
    public String modifyPsw(UpdPwd updPwd) {
        Staff o =(Staff) TokenUtil.data.get();
        if(updPwd.getCpsw().equals(updPwd.getPsw())){
            Staff staff = new Staff();
            staff.setId(o.getId());
            staff.setPsw(updPwd.getPsw());
            int updatePwd = staffMapper.updateById(staff);
            if(updatePwd>0){
                return "修改密码成功";
            }
        }
        return "修改密码失败";
    }

    @Override
    public String updUserMsg(UpdPwd updPwd) {
        Staff staff =(Staff) TokenUtil.data.get();
        updPwd.setPsw(null);
        Staff user = new Staff();
        user.setId(staff.getId());
        user.setPhone(updPwd.getPhone());
        user.setEmail(updPwd.getEmail());
        int i = staffMapper.updateById(user);
        if(i>0){
            return "修改用户信息成功";
        }
        return "修改用户信息失败";
    }

    @Override
    public String register(Staff staff) {
        String psw = staff.getPsw();
        String rpsw = staff.getRpsw();
        if(psw.equals(rpsw)){
            Staff staff1 = new Staff();
            staff1.setEmail(staff.getEmail());
            staff1.setPhone(staff.getPhone());
            staff1.setPsw(psw);
            LocalDateTime now = LocalDateTime.now();
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            staff1.setTime(format);
            int insert = staffMapper.insert(staff);
            if(insert>0){
                return "注册成功";
            }
        }
        return "注册失败";
    }
}
